import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Spin,
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Dropdown,
  Menu,
  InputNumber,
  DatePicker,
  Modal,
  message,
  Badge,
  Divider,
  Steps,
  Radio,
  Upload,
} from 'antd';
import moment from 'moment';
import router from 'umi/router';

import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

import styles from './Test.less';

const FormItem = Form.Item;
const { Step } = Steps;
const { TextArea } = Input;
const { Option } = Select;
const RadioGroup = Radio.Group;
const SelectOption = Select.Option;
const getValue = obj =>
  Object.keys(obj)
    .map(key => obj[key])
    .join(',');

@connect(({ rule, loading }) => ({
  rule,
  loading: loading.models.ruleTest,
}))
@Form.create()
class ThreeList extends PureComponent {
  state = {
    formValues: {
      codeList: [],
      type: 3,
    }, // 查询参数
    listLoading: false, // 加载状态
    selectedRows: [], // 默认参数
    modalVisible: false, // 新增、修改页面是否显示
    imageUrl: '',
    imageUrlzbf: '',
    loading: false,

    textboxValues: {
      key: '',
      type: 1,
    },
    suggestions: [],

    filterData: [],
    expandForm: false, // 查询菜单栏 是否显示

    updateModalVisible: false,

    stepFormValues: {},

    dataTradeList: {},
    updateParam: true,

    premiumrate: '100%',
  };

  columns3 = [
    /*    {
      title: '初始',
      dataIndex: 'id',
    }, */
    {
      title: '代码',
      dataIndex: 'symbol',
    },
    {
      title: '名称',
      dataIndex: 'name',
    },
    {
      title: '总市值',
      dataIndex: 'market',
    },
    {
      title: '9折',
      dataIndex: 'p90',
    },
    {
      title: '85折',
      dataIndex: 'p85',
    },
    {
      title: '8折',
      dataIndex: 'p80',
    },
    {
      title: '75折',
      dataIndex: 'p75',
    },
    {
      title: '7折',
      dataIndex: 'p70',
    },
    {
      title: '行业名称',
      dataIndex: 'tradename',
    },
    {
      title: '市盈率',
      dataIndex: 'ttm',
    },
    {
      title: '市净率',
      dataIndex: 'pb',
    },
  ];

  columns6 = [
    /*    {
      title: '初始',
      dataIndex: 'id',
    }, */
    {
      title: '代码',
      dataIndex: 'symbol',
    },
    {
      title: '名称',
      dataIndex: 'name',
    },
    {
      title: '总市值',
      dataIndex: 'market',
    },
    {
      title: '9折',
      dataIndex: 'p90',
    },
    {
      title: '85折',
      dataIndex: 'p85',
    },
    {
      title: '8折',
      dataIndex: 'p80',
    },
    {
      title: '75折',
      dataIndex: 'p75',
    },
    {
      title: '7折',
      dataIndex: 'p70',
    },
    {
      title: '行业名称',
      dataIndex: 'tradename',
    },
    {
      title: '市盈率',
      dataIndex: 'ttm',
    },
    {
      title: '市净率',
      dataIndex: 'pb',
    },
  ];

  columns12 = [
    /*    {
      title: '初始',
      dataIndex: 'id',
    }, */
    {
      title: '代码',
      dataIndex: 'symbol',
    },
    {
      title: '名称',
      dataIndex: 'name',
    },
    {
      title: '总市值',
      dataIndex: 'market',
    },
    {
      title: '9折',
      dataIndex: 'p90',
    },
    {
      title: '85折',
      dataIndex: 'p85',
    },
    {
      title: '8折',
      dataIndex: 'p80',
    },
    {
      title: '75折',
      dataIndex: 'p75',
    },
    {
      title: '7折',
      dataIndex: 'p70',
    },
    {
      title: '行业名称',
      dataIndex: 'tradename',
    },
    {
      title: '市盈率',
      dataIndex: 'ttm',
    },
    {
      title: '市净率',
      dataIndex: 'pb',
    },
  ];

  // 默认加载数据-起始方法
  render() {
    const {
      rule: { threedata, sixdata, otherdata },
      loading,
      total,
    } = this.props;
    const { selectedRows, listLoading, modalVisible } = this.state;
    const newdata3 = {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    };
    const newdata6 = {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    };

    const newdata12 = {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    };
    if (threedata.code == 1 && threedata.data != null) {
      newdata3.list = threedata.data;
      // newdata.pagination = data.paginationModel
    }
    if (sixdata.code == 1 && sixdata.data != null) {
      newdata6.list = sixdata.data;
      // newdata.pagination = data.paginationModel
    }
    if (otherdata.code == 1 && otherdata.data != null) {
      newdata12.list = otherdata.data;
      // newdata.pagination = data.paginationModel
    }
    return (
      <PageHeaderWrapper title="RCN票据">
        <div className={styles.tableListForm}>{this.renderForm()}</div>
        <Card bordered={false}>
          <h3 style={{ marginBottom: 16 }}>三个月票据</h3>
          <div className={styles.tableList}>
            <StandardTable
              selectedRows={selectedRows}
              loading={listLoading}
              data={newdata3}
              columns={this.columns3}
              pagination={false}
              onChange={this.handleStandardTableChange}
              rowKey="id"
              scroll={{ x: 'auto' }}
            />
          </div>
        </Card>

        <Card bordered={false}>
          <h3 style={{ marginBottom: 16 }}>六个月票据</h3>
          <div className={styles.tableList}>
            <StandardTable
              selectedRows={selectedRows}
              loading={listLoading}
              data={newdata6}
              columns={this.columns6}
              pagination={false}
              onChange={this.handleStandardTableChange}
              rowKey="id"
              scroll={{ x: 'auto' }}
            />
          </div>
        </Card>

        <Card bordered={false}>
          <h3 style={{ marginBottom: 16 }}>十二个月票据</h3>
          <div className={styles.tableList}>
            <StandardTable
              selectedRows={selectedRows}
              loading={listLoading}
              data={newdata12}
              columns={this.columns12}
              pagination={false}
              onChange={this.handleStandardTableChange}
              rowKey="id"
              scroll={{ x: 'auto' }}
            />
          </div>
        </Card>
      </PageHeaderWrapper>
    );
  }

  // 加载完 之后 再加载 此 方法
  componentDidMount() {
    const { dispatch } = this.props;
    const { formValues } = this.state;
    /*    dispatch({
      type: 'rule/GetThreeBillList',
      payload: formValues,
    }); */
  }

  // 改变每页条数和当前页
  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;
    formValues.pageNum = pagination.current;
    formValues.pageSize = pagination.pageSize;
    const params = {
      ...formValues,
    };
    dispatch({
      type: 'activity/GetActivityList',
      payload: params,
    });
  };

  // 判断 菜单栏 是 缩放显示 还是 全部展示
  renderForm() {
    const { expandForm } = this.state;
    return expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
  }

  // 菜单栏- 点击展开显示 事件
  renderAdvancedForm() {
    const {
      form: { getFieldDecorator },
    } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="最大财报日">
              {getFieldDecorator('maxEarningsDate ')(
                <DatePicker style={{ width: '100%' }} placeholder="请输入最大财报日" />
              )}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="最小财报日">
              {getFieldDecorator('minEarningsDate')(
                <DatePicker style={{ width: '100%' }} placeholder="请输入最小财报日" />
              )}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="最小保本幅度">
              {getFieldDecorator('minBreakEvenMargin')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
        </Row>
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="最小权利金">
              {getFieldDecorator('minRoyalty')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="最小收益率">
              {getFieldDecorator('minYieldRate')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
        </Row>
        <div style={{ overflow: 'hidden' }}>
          <div style={{ marginBottom: 24 }}>
            <Button type="primary" htmlType="submit">
              查询
            </Button>
            <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
              重置
            </Button>
            <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
              收起 <Icon type="up" />
            </a>
          </div>
        </div>
      </Form>
    );
  }

  // 菜单栏 默认收缩显示 事件
  renderSimpleForm() {
    const {
      form: { getFieldDecorator },
    } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={6} sm={24}>
            <FormItem label="股票代码">
              {/*              {getFieldDecorator('code',{ rules: [{ required: true, message: '请输入活动名称！', min: 1 }],})(
                <Input placeholder="请输入"/>
              )} */}

              {getFieldDecorator('codeList')(
                <Select
                  mode="multiple"
                  showSearch
                  style={{ width: '100%' }}
                  placeholder="请输入股票代码"
                  onSearch={this.onSearch.bind(this)}
                  /*  onSelect={this.onSelect.bind(this)} */
                >
                  {this.state.suggestions}
                </Select>
              )}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <span className={styles.submitButtons}>
              <Button type="primary" htmlType="submit">
                查询
              </Button>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  // 菜单栏-查询按钮 事件
  handleSearch = e => {
    e.preventDefault();
    const {
      dispatch,
      form,
      rule: { threedata },
    } = this.props;
    const { formValues } = this.state;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      form.resetFields();
      dispatch({
        type: 'rule/GetThreeBillList',
        payload: {
          codeList: fieldsValue.codeList,
          type: 3,
        },
      });

      dispatch({
        type: 'rule/GetSixBillList',
        payload: {
          codeList: fieldsValue.codeList,
          type: 6,
        },
      });

      dispatch({
        type: 'rule/GetOtherBillList',
        payload: {
          codeList: fieldsValue.codeList,
          type: 12,
        },
      });
    });
  };

  // 菜单栏- 重置按钮 事件
  handleFormReset = () => {
    const { form, dispatch } = this.props;
    const { formValues } = this.state;
    form.resetFields();
    formValues.maxEarningsDate = '';
    formValues.minBreakEvenMargin = 0;
    formValues.minEarningsDate = '';
    formValues.minRoyalty = 0;
    formValues.minYieldRate = 0;

    dispatch({
      type: 'fund/GetRoyaltiesList',
      payload: formValues,
    });
  };

  // 菜单栏- 展开 关闭 事件
  toggleForm = () => {
    const { expandForm } = this.state;
    this.setState({
      expandForm: !expandForm,
    });
  };

  onSearch(value) {
    /*    const {
          rule: {filterData},
        } = this.props; */
    const { textboxValues, textLoading, suggestions } = this.state;
    this.state.textboxValues.key = value;
    const _this = this;
    // this.state.suggestions=[];
    if (value.length > 0) {
      this.props.dispatch({
        type: 'rule/GetTextBoxList',
        payload: textboxValues,
        callback: () => {
          const suggestionsData = [];
          for (let i = 0; i < _this.props.rule.filterData.list.length; i++) {
            suggestionsData.push(
              <Option key={_this.props.rule.filterData.list[i].address}>
                {_this.props.rule.filterData.list[i].value}
              </Option>
            );
          }
          _this.setState({
            suggestions: suggestionsData,
          });
        },
      });
    }
  }
}

export default ThreeList;
